package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.packers.PickResultEntity;
import com.sgb.kjwl.view.ui.activity.packers.FindPackersActivity;
import com.sgb.link.view.adapter.PickAdapter;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

public class FindPackerPopWindow extends BasePopWindow {
    private FindPackersActivity activity;
    private FilterResultClick filterResultClick;

    private TextView okView;
    private TextView reSetView;
    private MaxRecyclerView rvFirst;
    private MaxRecyclerView rvSecond;
    private MaxRecyclerView rvThird;
    private MaxRecyclerView rvFourth;
    private MaxRecyclerView rvFifth;
    private MaxRecyclerView rvSixth;

    //工程分类 adapter
    private PickAdapter firstCategoryAdapter;
    private PickAdapter secondCategoryAdapter;
    private PickAdapter thirdCategoryAdapter;
    private PickAdapter fourthCategoryAdapter;
    private PickAdapter fifthCategoryAdapter;
    private PickAdapter sixthCategoryAdapter;

    private PickResultEntity pickResultProject;
    private PickResultEntity pickResultMajor;
    private String thirdCategoryNo = "";
    private String fourthCategoryNo = "";
    private String fifthCategoryNo = "";
    private String sixthCategoryNo = "";


    public FindPackerPopWindow(Context context) {
        super(context, DIR_RIGHT);
        activity = (FindPackersActivity) context;
    }

    @Override
    protected int popLayout() {
        return R.layout.find_packs_popwindow;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        if (pickResultProject == null){
            pickResultProject = new PickResultEntity();
        }
        if (pickResultMajor == null){
            pickResultMajor = new PickResultEntity();
        }

        okView = holder.getPop().findViewById(R.id.ok_view);
        reSetView = holder.getPop().findViewById(R.id.reset_view);

        //工程分类rv
        rvFirst = holder.getPop().findViewById(R.id.rv_first_category);
        //工程专项rv
        rvSecond = holder.getPop().findViewById(R.id.rv_second_category);
        //专业分类rv
        rvThird = holder.getPop().findViewById(R.id.rv_third_category);
        //分项工程rv
        rvFourth = holder.getPop().findViewById(R.id.rv_fourth_category);
        //子项工程rv
        rvFifth = holder.getPop().findViewById(R.id.rv_fifth_category);
        //子分项工程rv
        rvSixth = holder.getPop().findViewById(R.id.rv_sixth_category);

        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                filterResultClick.result(pickResultProject, pickResultMajor, thirdCategoryNo, fourthCategoryNo, fifthCategoryNo, sixthCategoryNo);
                dissMiss();
            }
        });

        reSetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                firstCategoryAdapter.resetCheckDatas();
                secondCategoryAdapter.resetCheckDatas();
                thirdCategoryAdapter.resetCheckDatas();
                fourthCategoryAdapter.resetCheckDatas();
                fifthCategoryAdapter.resetCheckDatas();
                sixthCategoryAdapter.resetCheckDatas();

                firstCategoryAdapter.setDefItem(-1);
                secondCategoryAdapter.setDefItem(-1);
                thirdCategoryAdapter.setDefItem(-1);
                fourthCategoryAdapter.setDefItem(-1);
                fifthCategoryAdapter.setDefItem(-1);
                sixthCategoryAdapter.setDefItem(-1);
            }
        });

        firstCategoryAdapter = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        firstCategoryAdapter.setItemSelect(true);
        firstCategoryAdapter.setDefItem(-1);
        rvFirst.setLayoutManager(new GridLayoutManager(mContext, 3));
        rvFirst.setAdapter(firstCategoryAdapter);
        firstCategoryAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                pickResultProject.setNo(pickSelectEntity.getNo());
                pickResultProject.setName(pickSelectEntity.getProjectscale());
                pickResultProject.setPosition(position);
                pickResultMajor.clear();
                thirdCategoryNo = "";
                fourthCategoryNo = "";
                fifthCategoryNo = "";
                sixthCategoryNo = "";

                secondCategoryAdapter.clearDatas();
                thirdCategoryAdapter.clearDatas();
                fourthCategoryAdapter.clearDatas();
                fifthCategoryAdapter.clearDatas();
                sixthCategoryAdapter.clearDatas();
                if (!TextUtils.isEmpty(pickSelectEntity.getNo())) {
                    //activity.viewModel.getSecondCategory(pickSelectEntity.getNo(), true);
                }

            }
        });

        secondCategoryAdapter = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        secondCategoryAdapter.setItemSelect(true);
        secondCategoryAdapter.setDefItem(-1);
        rvSecond.setLayoutManager(new GridLayoutManager(mContext, 3));
        rvSecond.setAdapter(secondCategoryAdapter);
        secondCategoryAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                pickResultMajor.setNo(pickSelectEntity.getNo());
                pickResultMajor.setName(pickSelectEntity.getProjectscale());
                pickResultMajor.setPosition(position);
                thirdCategoryNo = "";
                fourthCategoryNo = "";
                fifthCategoryNo = "";
                sixthCategoryNo = "";

                thirdCategoryAdapter.clearDatas();
                fourthCategoryAdapter.clearDatas();
                fifthCategoryAdapter.clearDatas();
                sixthCategoryAdapter.clearDatas();
                if (!TextUtils.isEmpty(pickSelectEntity.getNo())) {
                   //activity.viewModel.getThrCategory(pickSelectEntity.getNo(), true);
                }

            }
        });

        thirdCategoryAdapter = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        thirdCategoryAdapter.setItemSelect(true);
        thirdCategoryAdapter.setDefItem(-1);
        rvThird.setLayoutManager(new GridLayoutManager(mContext, 3));
        rvThird.setAdapter(thirdCategoryAdapter);
        thirdCategoryAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                thirdCategoryNo = pickSelectEntity.getNo();
                fourthCategoryNo = "";
                fifthCategoryNo = "";
                sixthCategoryNo = "";

                fourthCategoryAdapter.clearDatas();
                fifthCategoryAdapter.clearDatas();
                sixthCategoryAdapter.clearDatas();
                if (!TextUtils.isEmpty(pickSelectEntity.getNo())) {
                    //activity.viewModel.getFourCategory(pickSelectEntity.getNo(), true);
                }
            }
        });

        fourthCategoryAdapter = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        fourthCategoryAdapter.setItemSelect(true);
        fourthCategoryAdapter.setDefItem(-1);
        rvFourth.setLayoutManager(new GridLayoutManager(mContext, 3));
        rvFourth.setAdapter(fourthCategoryAdapter);
        fourthCategoryAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                fourthCategoryNo = pickSelectEntity.getNo();
                fifthCategoryNo = "";
                sixthCategoryNo = "";

                fifthCategoryAdapter.clearDatas();
                sixthCategoryAdapter.clearDatas();
                if (!TextUtils.isEmpty(pickSelectEntity.getNo())) {
                    //activity.viewModel.getFiveCategory(pickSelectEntity.getNo(), true);
                }
            }
        });

        fifthCategoryAdapter = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        fifthCategoryAdapter.setItemSelect(true);
        fifthCategoryAdapter.setDefItem(-1);
        rvFifth.setLayoutManager(new GridLayoutManager(mContext, 3));
        rvFifth.setAdapter(fifthCategoryAdapter);
        fifthCategoryAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                fifthCategoryNo = pickSelectEntity.getNo();
                sixthCategoryNo = "";

                sixthCategoryAdapter.clearDatas();
                if (!TextUtils.isEmpty(pickSelectEntity.getNo())) {
                    //activity.viewModel.getSixCategory(pickSelectEntity.getNo(), true);
                }
            }
        });

        sixthCategoryAdapter = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        sixthCategoryAdapter.setItemSelect(true);
        sixthCategoryAdapter.setDefItem(-1);
        rvSixth.setLayoutManager(new GridLayoutManager(mContext, 3));
        rvSixth.setAdapter(sixthCategoryAdapter);
        sixthCategoryAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                sixthCategoryNo = pickSelectEntity.getNo();
            }
        });
    }

    public void setFirstData(List<PickSelectEntity> data) {
        firstCategoryAdapter.setDatas(data);
        firstCategoryAdapter.setDefItem(-1);
    }

    public void setSecondData(List<PickSelectEntity> data) {
        secondCategoryAdapter.setDatas(data);
        secondCategoryAdapter.setDefItem(-1);
    }

    public void setThirdData(List<PickSelectEntity> data) {
        thirdCategoryAdapter.setDatas(data);
        thirdCategoryAdapter.setDefItem(-1);
    }

    public void setFourthData(List<PickSelectEntity> data) {
        fourthCategoryAdapter.setDatas(data);
        fourthCategoryAdapter.setDefItem(-1);
    }

    public void setFifthData(List<PickSelectEntity> data) {
        fifthCategoryAdapter.setDatas(data);
        fifthCategoryAdapter.setDefItem(-1);
    }

    public void setSixthData(List<PickSelectEntity> data) {
        sixthCategoryAdapter.setDatas(data);
        sixthCategoryAdapter.setDefItem(-1);
    }

    public interface FilterResultClick {

        void result(PickResultEntity first, PickResultEntity second, String third, String fourth, String fifth, String sixth);

        void dimiss();
    }

    public void setFilterResultClick(FilterResultClick filterResultClick) {
        this.filterResultClick = filterResultClick;
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (filterResultClick != null) {
            filterResultClick.dimiss();
        }
    }
}
